package iwb.utils.localpatition;

import com.google.common.collect.Lists;
import lombok.Data;
import org.springframework.scheduling.annotation.Async;

import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;


@Data
public abstract class LocalPartition<T> {
    protected LinkedBlockingQueue<List<T>> queue = new LinkedBlockingQueue();

    protected AtomicInteger size = null;


    @Async
    public void putAndExecList(List<T> list) {
        partitionExec(list);
        queue.add(list);
    }

    public abstract void partitionExec(List<T> list);

    public List<T> getList() {
        List<T> list = Lists.newLinkedList();
        try {
            list = queue.take();

        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            size.getAndDecrement();
            return list;
        }
    }


    public boolean isRunOver(){
        return size.get() != 0;
    }

}
